﻿using System.Collections.Specialized;
using System.Configuration;
namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// 配置文件 Section节点操作帮助类
    /// </summary>
    public class SectionHelper
    {
        #region 构造函数以及私有变量
        NameValueCollection ModulSettings = null;
        /// <summary>
        ///构造函数 
        /// </summary>
        /// <param name="sectionName">section名称</param>
        public SectionHelper(string sectionName)
        {
            ModulSettings = ConfigurationManager.GetSection(sectionName) as NameValueCollection;
        }
        #endregion
        #region 是否包含该Section
        /// <summary>
        /// 是否包含该Section
        /// </summary>
        /// <returns></returns>
        public bool ContainSection()
        {
            return !(ModulSettings == null);
        }
        #endregion 
        #region Section是否包含Key
        /// <summary>
        /// Section是否包含Key
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public bool ContainKey(string key)
        {
            if (ContainSection())
            {
                return !(ModulSettings[key] == null);
            }
            return false;
        }
        #endregion 
        #region 根据键获取值
        /// <summary>
        /// 根据键获取值
        /// </summary>
        /// <param name="Key">键</param>
        /// <returns>当不存在键的时候，返回string.Empty</returns>
        public string GetValue(string Key)
        {
            string _value = string.Empty;
            if (ContainKey(Key))
            {
                _value = ModulSettings[Key];
            }
            return _value;
        }
        #endregion 
    }
}